package com.example.mycamera1;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import java.io.IOException;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private Camera mCamera;
    private Camera.Size mSize;

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        startPreview();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

    }
//    byte[] buffer;
    private void startPreview(){
        mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        Camera.Parameters parameters = mCamera.getParameters();
        mSize = parameters.getPreviewSize();
        try {
            mCamera.setPreviewDisplay(getHolder());
//            buffer = new byte[mSize.width * mSize.height * 3 / 2];
//            mCamera.addCallbackBuffer(buffer);
            mCamera.setDisplayOrientation(90);
            mCamera.startPreview();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
